package com.example.androidclass;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class Schedule extends AppCompatActivity {

    private static final String TAG = "Click";
    public static List<oneClass> class_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule);
        ImageView jump_to_timeline = (ImageView) findViewById(R.id.jump_to_timeline);
        jump_to_timeline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Schedule.this, Timeline.class);
                startActivity(intent);
            }
        });

//        隐藏默认标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

//        解析Json
        String json_data = getJson("class.json", this);
        class_list = parseJSONWithGSON(json_data);

//        对每一个解析过后的课程时间进行绘制
        for (oneClass one_class : class_list) {
            drawTable(one_class);
        }
    }


//    绘制课表的函数，主要逻辑为获取每这一门课是第几节课，如果为两节课，则删除其中一个TextView，并设置剩下的一个TextView的Layout_Weight为2
    public void drawTable(oneClass one_class) {
        String day_of_week = one_class.getDay();
        int[] class_time = one_class.getTime();
        String name = one_class.getName();
        String place = one_class.getClassroom();
        int start_time = class_time[0];
        int end_time = class_time[1];
        String startId = "time_" + day_of_week + "_" + start_time;
//        删除多余的TextView，只剩下第一个TextView
        if (start_time != end_time) {
            for (int i = start_time + 1; i <= end_time; i++) {
                int delete_time = i;
                String delete_id = "time_" + day_of_week + "_" + delete_time;
                deleteView(delete_id);
            }
        }
//        设置剩下的一个TextView的weight为上课时间的长度，并显示课程信息
        TextView textView = findViewByStringId(startId);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1 + end_time - start_time));
        textView.setText(name + "@" + place);
    }


//    根据Id查找到view，因为根据findViewById(R.id.time_1_1)这种方式来查找，那个time_1_1其实是一个数字，而deleteId是一个字符串，不能用这种方法查找
    public <T> T findViewByStringId(String id) {
        int resId = getResources().getIdentifier(id, "id", getPackageName());
        T view = (T) findViewById(resId);
        return view;
    }


//    根据id删除view
    public void deleteView(String id) {
        TextView textView = findViewByStringId(id);
        ViewGroup parent = (ViewGroup) textView.getParent();
        parent.removeView(textView);
    }


//    Json处理之后的时间表
     class oneClass {
        private String day_of_week;
        private int[] class_time;
        private String name;
        private String place;
        private String time;
        private String date;

        String getDay() {return this.day_of_week;}
        int[] getTime() {return this.class_time;}
        String getName() {return this.name;}
        String getClassroom() {return this.place;}
        String getReal_time() {return  this.time;}
        String getDate() {return this.date;}
    }


//    用OKHttp请求Json文件
//    private void sendRequestWithOkHttp() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    OkHttpClient client = new OkHttpClient();
//                    Request request = new Request.Builder().url("http://localhost/get_data.json").build();
//                    Response response = client.newCall(request).execute();
//                    String responseData = response.body().string();
//                    parseJSONWithGSON(responseData);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//    }


//    本地读取assets目录下的Json文件
    public static String getJson(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


//    用Gson转换Json文件
    public static List<oneClass> parseJSONWithGSON(String jsonData) {
        Gson gson = new Gson();
        List<oneClass> timeList = gson.fromJson(jsonData, new TypeToken<List<oneClass>>(){}.getType());
        return timeList;
    }
}
